<?php
/**
 * Created by xun at 2020/5/22
 */

$first = "亲爱的【{姓名}】，刚有客户查看了您的文章";
$remark = "请点击“这里”或“客户线索”，把握客户机会！";

$data = [
    'touser' => '1',
    'template_id' => 'tmp1',
    'url' => 'http://www.google.com',
    'data' => [
        'first' => [
            'value' => $first,
            'color' => '#000'
        ],
        'keyword1' => [
            'value' => 'pyl',
            'color' => '#000000',
        ],
        'keyword2' => [
            'value' => '15622965560',
            'color' => '#000000',
        ],
        'keyword3' => [
            'value' => date('Y-m-d H:i:s', time()),
            'color' => '#000000',
        ],
        'remark' => [
            'value' => $remark,
            'color' => '#000',
        ]
    ],
];

$params = [
    [
        'parameter_name' => 'keyword1',
        'parameter_value' => '{姓名}',
    ],
    [
        'parameter_name' => 'keyword2',
        'parameter_value' => '{联系方式}',
    ],
    [
        'parameter_name' => 'keyword3',
        'parameter_value' => '{咨询时间}',
    ],
];

// todo
foreach ($params as $param) {
    $first = str_replace($param['parameter_value'], $data['data'][$param['parameter_name']]['value'], $first);
    $remark = str_replace($param['parameter_value'], $data['data'][$param['parameter_name']]['value'], $remark);
}

$data['data']['first'] = [
    'value' => $first,
    'color' => '#000',
];

$data['data']['remark'] = [
    'value' => $remark,
    'color' => '#000',
];

print_r(json_encode($data, JSON_UNESCAPED_UNICODE));